﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using My_TV_Manager.Model;
using My_TV_Manager.View.MenuButton;
using My_TV_Manager.View.ContentPanel;

namespace My_TV_Manager.View.MenuBar
{
    [System.ComponentModel.DesignerCategory("")]

    public class OuterMenuBar : TabbedMenuBar
    {
        #region Private Data

        private Controller controller;

        private OuterTabbedContainer parent;
        private OuterTabbedButton logoButton;

        #endregion

        #region Properties

        public override Size ContainerSize
        {
            get { return this.Size; }
            set
            {
                this.Size = value;
                int remainingPixels = this.Height;
                for (int i = 0; i < buttons.Count; i++)
                {
                    buttons[i].Size = new Size(this.Width, 
                                               remainingPixels / (buttons.Count - i));
                    remainingPixels -= buttons[i].Height;
                }
            }
        }

        #endregion

        #region Constructors

        public OuterMenuBar(OuterTabbedContainer parent, Size size, List<string> buttonText) 
            : base(size)
        {
            this.parent = parent;

            int remainingPixels = this.Height;

            logoButton = new OuterTabbedButton();
            logoButton.Name = "Logo Button";
            logoButton.Size = new Size(this.Width,
                                       remainingPixels / (buttonText.Count + 1));
            logoButton.Click += new EventHandler(MenuOptionClick);
            logoButton.Image = new Bitmap("C:\\Users\\Geoff\\Desktop\\logo.png");

            remainingPixels -= logoButton.Height;
            
            this.Controls.Add(logoButton);
            
            for (int i = 0; i < buttonText.Count; i++)
            {
                OuterTabbedButton button = new OuterTabbedButton();
                
                button.Text = buttonText[i];
                button.Name = "Main Menu Option " + i;
                button.ForeColor = Settings.Colors.Get(i);
                button.Size = new Size(this.Width, 
                                       remainingPixels / (buttonText.Count - i));
                button.Click += new EventHandler(MenuOptionClick);

                remainingPixels -= button.Height;

                buttons.Add(button);
                this.Controls.Add(button);
            }

            selected = -1;
            buttons[0].PerformClick();
        }

        public OuterMenuBar(OuterTabbedContainer parent, Size size, 
                            List<string> buttonText, Controller controller)
            : this(parent, size, buttonText)
        {
            this.controller = controller;

            buttons[controller.InitialPanelIndex].PerformClick();
        }

        #endregion

        #region Functions

        protected override void MenuOptionClick(object sender, EventArgs e)
        {
            OuterTabbedButton button = (OuterTabbedButton)sender;

            int previouslySelected = selected;

            int newContainer;

            // Find the index of the new content panel
            if (button.Name == "Logo Button")
            {
                newContainer = controller.LogoPanelIndex;
            }
            else
            {
                newContainer = int.Parse(button.Name.Substring(button.Name.Length - 1));
            }

            // Deselect the previously selected option
            if (selected >= 0)
            {
                buttons[selected].BackColor = Color.White;
                buttons[selected].ForeColor = Settings.Colors.Get(selected);
            }

            selected = newContainer;

            // Change the back colors
            buttons[selected].BackColor = Settings.Colors.Get(selected);
            buttons[selected].ForeColor = Color.Black;

            if (selected == previouslySelected) { return; }
            
            // Change the content panel in the split container
            parent.ChangeContentContainer(selected);
        }

        #endregion
    }
}
